package com.lv.LvAttachmentComboOfTourismModel.web;
import com.aqgj.User.bean.User;
import com.lv.LvAttachmentComboOfTourismModel.bean.LvAttachmentComboOfTourismModel;
import com.lv.LvAttachmentComboOfTourismModel.service.LvAttachmentComboOfTourismModelManager;
import com.lv.LvPriceComboOfTourismModel.bean.LvPriceComboOfTourismModel;
import com.lv.LvTourismModel.bean.LvTourismModel;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
@Controller
@RequestMapping("lvAttachmentComboOfTourismModelAction")
public class LvAttachmentComboOfTourismModelAction {
    @Autowired
    private LvAttachmentComboOfTourismModelManager lvAttachmentComboOfTourismModelManager;
    private static final Logger log = LogManager.getLogger(LvTourismModel.class);
    @Autowired
    private ZtcTransportManager ztcTransportManager;

    /**
     * 添加旅游模板
     * @param lvAttachmentComboOfTourismModel 旅游模板附件对象
     * @param lvTourismModelId 旅游模板id
     * @param session
     * @return
     */
    @RequestMapping("sign")
    @ResponseBody
    public HashMap<String, Object> save(LvAttachmentComboOfTourismModel lvAttachmentComboOfTourismModel, String lvTourismModelId, HttpSession session){
        log.info("开始添加");
        HashMap<String, Object> wapper = new HashMap<>();
        User user = (User) session.getAttribute("userInfo");
        if (user == null || "".equals(user)) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "用户未登录");
            log.info("用户未登录");
            return wapper;
        }
        ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
        int transportType = transport.getType();
        if (transportType == 0 || transportType == 1 || transportType == 2) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "非旅游公司不可使用此功能");
            log.info("非旅游公司不可使用此功能");
            return wapper;
        }
        if (transport.getStatus() == 4) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "该用户公司已被冻结，不可使用此功能");
            log.info("该用户公司已被冻结，不可使用此功能");
            return wapper;
        }
        String result=lvAttachmentComboOfTourismModelManager.save(lvAttachmentComboOfTourismModel,lvTourismModelId,user);
        if("0".equals(result)){
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "添加失败");
            log.info("添加失败");
            return wapper;
        }
        wapper.put("success", Boolean.valueOf(true));
        wapper.put("data", "添加成功");
        log.info("添加成功");
        return wapper;
    }

    /**
     * 修改旅游模板附件
     * @param lvAttachmentComboOfTourismModel 旅游模板附件对象
     * @param session
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public HashMap<String, Object> update(LvAttachmentComboOfTourismModel lvAttachmentComboOfTourismModel, HttpSession session){
        log.info("开始修改");
        HashMap<String, Object> wapper = new HashMap<>();
        User user = (User) session.getAttribute("userInfo");
        if (user == null || "".equals(user)) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "用户未登录");
            log.info("用户未登录");
            return wapper;
        }
        ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
        int transportType = transport.getType();
        if (transportType == 0 || transportType == 1 || transportType == 2) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "非旅游公司不可使用此功能");
            log.info("非旅游公司不可使用此功能");
            return wapper;
        }
        if (transport.getStatus() == 4) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "该用户公司已被冻结，不可使用此功能");
            log.info("该用户公司已被冻结，不可使用此功能");
            return wapper;
        }
        String result=lvAttachmentComboOfTourismModelManager.update(lvAttachmentComboOfTourismModel,user);
        if("0".equals(result)){
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "修改失败");
            log.info("修改失败");
            return wapper;
        }
        wapper.put("success", Boolean.valueOf(true));
        wapper.put("data", "修改成功");
        log.info("修改成功");
        return wapper;
    }

    /**
     * 删除旅游模板
     * @param id 旅游模板id
     * @param session
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public HashMap<String, Object> delete(String id, HttpSession session){
        log.info("开始删除");
        HashMap<String, Object> wapper = new HashMap<>();
        User user = (User) session.getAttribute("userInfo");
        if (user == null || "".equals(user)) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "用户未登录");
            log.info("用户未登录");
            return wapper;
        }
        ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
        int transportType = transport.getType();
        if (transportType == 0 || transportType == 1 || transportType == 2) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "非旅游公司不可使用此功能");
            log.info("非旅游公司不可使用此功能");
            return wapper;
        }
        if (transport.getStatus() == 4) {
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "该用户公司已被冻结，不可使用此功能");
            log.info("该用户公司已被冻结，不可使用此功能");
            return wapper;
        }
        String result=lvAttachmentComboOfTourismModelManager.delete(id);
        if("0".equals(result)){
            wapper.put("success", Boolean.valueOf(false));
            wapper.put("msg", "删除失败");
            log.info("删除失败");
            return wapper;
        }
        wapper.put("success", Boolean.valueOf(true));
        wapper.put("data", "删除成功");
        log.info("删除成功");
        return wapper;
    }
}